///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.share.clearspring {
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.utils.ObjectUtil;
	
	import sugarcookie.events.GenericEvent;

	public class Kernel extends Sprite {
		private var _isReady:Boolean = false;
		private var _log:ILogger;
		
		private var kernelUri:String;
		private var kernel:Object;
		private var kernelLoader:Loader;
		
		public function Kernel(uri:String) {
			super();
			_log = Log.getLogger("share.clearspring.Kernel");
			kernelUri = uri;
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		
		public function get isReady():Boolean {
			return _isReady;
		}
		
		public function showMenu():Boolean {
			if (!_isReady) {
				return false;
			} else {
				kernel.menu.configure({videoid:0});
				kernel.menu.show();
				return true;
			}
		}
		
		public function hideMenu():void {
			kernel.menu.hide();
		}
		
		private function init(evt:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE,init);
			
			Security.allowDomain("bin.clearspring.com");
			Security.allowDomain("widgets.clearspring.com");
			
			kernelLoader = new Loader();
			kernelLoader.addEventListener('on_ready', onAPILoad);
			addChild(kernelLoader);

			kernelLoader.load(new URLRequest(kernelUri),new LoaderContext(false, ApplicationDomain.currentDomain, SecurityDomain.currentDomain));
		}
		
		private function onAPILoad(evt:Event):void {
			kernel = Object(kernelLoader.content).kernel;
			kernel.menu.addEventListener(kernel.menu.event.SHARE, shareComplete);
			
			_isReady = true;
		}
		
		private function shareComplete(evt:Object):void {
			
		}
	}
}